package PowerWord.UI.Exercise;

import java.util.ArrayList;

import android.content.Context;
import android.graphics.Color;
import android.text.TextUtils.TruncateAt;
import android.text.method.ScrollingMovementMethod;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.LinearLayout.LayoutParams;
import PowerWord.Core.Plan.MultiExercise;
import PowerWord.UI.R;

public class MultiToMultiHelper extends ExerciseUIHelper {

	ArrayList<ColorButton> words = new ArrayList<ColorButton>();
	ArrayList<ColorButton> translations = new ArrayList<ColorButton>();
	M2MAnswerViewer answerViewer = null;
	int[] userAnswer = null;
	int lastClickId[] = {-1, -1};
	
	static final int[] colors = {0xFF00007F, 0xFF003F3F, 0xFF007F00,
								 0xFF3F003F, 0xFF7F0000, 0xFF3F3F00};
	int colorId = 0;
	
	ColorButton GetLastClickItem(){
		if(lastClickId[0] == 0){
			return words.get(lastClickId[1]);
		}
		else{
			return translations.get(lastClickId[1]);
		}
	}
	
	class ColorButton extends Button{
		
		int groupID = -1;
		int id = -1;
		boolean frozen = false;
		
		private void SetFrozen(boolean b){
			frozen = b;
		}
		
		protected void SetButtonID(int i, int j){
			groupID = i;
			id = j;
		}
		
		protected void Refresh(boolean needDisplay){
			setBackgroundColor(Color.BLACK);
			SetFrozen(false);
			setVisibility(needDisplay ? View.VISIBLE : View.GONE);
		}
		
		private void SetClickID(int i, int j){
			MultiToMultiHelper.this.lastClickId[0] = i;
			MultiToMultiHelper.this.lastClickId[1] = j;
		}
		
		@Override
		public boolean isFocused(){
			super.isFocused();
			return true;
		}
		
		public ColorButton(Context context) {
			super(context);
			setBackgroundColor(Color.BLACK);
			setTextColor(Color.WHITE);
			setSingleLine();
			setEllipsize(TruncateAt.MARQUEE);
			this.setMarqueeRepeatLimit(-1);
			LayoutParams params = new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT);
			params.setMargins(0, 5, 0, 5);
			params.weight = 1;
			setLayoutParams(params);
			this.setOnClickListener(new OnClickListener(){
				@Override
				public void onClick(View v) {
					if(frozen){
						return;
					}
					MultiToMultiHelper father = MultiToMultiHelper.this;
					if(father.lastClickId[0] == -1){
						setBackgroundColor(colors[father.colorId]);
						SetClickID(groupID, id);
					}
					else if(father.lastClickId[0] == groupID){
						if(father.lastClickId[1] == id){
							setBackgroundColor(Color.BLACK);
							SetClickID(-1, -1);
						}
						else{
							father.GetLastClickItem().setBackgroundColor(Color.BLACK);
							setBackgroundColor(colors[father.colorId]);
							SetClickID(groupID, id);
						}
					}
					else{
						setBackgroundColor(colors[father.colorId]);
						father.GetLastClickItem().SetFrozen(true);
						SetFrozen(true);
						if(father.lastClickId[0] == 0){
							userAnswer[id] = father.lastClickId[1];
						}
						else{
							userAnswer[father.lastClickId[1]] = id;
						}
						SetClickID(-1, -1);
						++father.colorId;
					}
				}
			});
		}
		
		
	}
	
	@Override
	protected void Init(ExerciseActivity context, int groupSize, boolean random){
		super.Init(context, groupSize, random);
		paper = new MultiExercise(groupSize);
		paper.SetGroupSize(groupSize);
		userAnswer = new int[groupSize];
		if(random){
			paper.Random();
		}
		answerViewer = new M2MAnswerViewer(father);
		LinearLayout answerViewerContainer = (LinearLayout)father.findViewById(R.id.M2MAnswerViewer);
		answerViewerContainer.removeAllViews();
		answerViewerContainer.addView(answerViewer);
		LinearLayout wordsGroup = (LinearLayout)father.findViewById(R.id.Words);
		LinearLayout transGroup = (LinearLayout)father.findViewById(R.id.Translations);
		wordsGroup.removeAllViews();
		transGroup.removeAllViews();
		for(int i=0; i<groupSize; ++i){
			ColorButton newWord = new ColorButton(father);
			newWord.setTextSize(20);
			newWord.SetButtonID(0, i);
			wordsGroup.addView(newWord);
			words.add(newWord);
			
			ColorButton newTran = new ColorButton(father);
			newTran.setTextSize(15);
			newTran.SetButtonID(1, i);
			transGroup.addView(newTran);
			translations.add(newTran);
		}
	}
	
	@Override
	protected void UpdateContent() {
		paper.Prepare();
		colorId = 0;
		lastClickId[0] = -1;
		lastClickId[1] = -1;
		int size = paper.GetCurGroupSize();
		MultiExercise mulPaper = (MultiExercise)paper;
		answerViewer.SetAnswer(size, mulPaper.GetAnswers());
		answerViewer.setVisibility(View.INVISIBLE);
		for(int i=0; i<words.size(); ++i){
			userAnswer[i] = -1;
			boolean needDisplay = i < size;
			ColorButton w = words.get(i);
			ColorButton t = translations.get(i);
			w.Refresh(needDisplay);
			t.Refresh(needDisplay);
			if(needDisplay){
				w.setText(mulPaper.GetWord(i));
				t.setText(mulPaper.GetTranslation(i));
			}
		}
	}

	@Override
	protected void ShowRightAnswer() {
		answerViewer.setVisibility(View.VISIBLE);
	}

	@Override
	protected void SaveScore() {
		((MultiExercise)paper).SaveScore(userAnswer);
	}
}
